﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TraceThisItem.Domain;
using System.Data;

namespace TraceThisItem.WebUI.Workflows
{
	public partial class Edit : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			var id = Convert.ToInt32(Request["id"]);
			var wf = Facade.Instance.GetWorkFlowById(id);

			litWfName.Text = wf.Name;
		}


		protected void btnAddNewNode_Click(object sender, EventArgs e)
		{
			var id = Convert.ToInt32(Request["id"]);
			var wf = Facade.Instance.GetWorkFlowById(id);

			wf.Nodes.Add(new StatusNode(NewStatusNodeName.Text));

			Facade.Instance.Save();

			EditorGrid.DataBind();
			CurrentNodes.DataBind();
		}

		
		protected void EditorGrid_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			e.Row.RowState = DataControlRowState.Edit;
		}

		protected void EditorGrid_RowCreated(object sender, GridViewRowEventArgs e)
		{
			e.Row.RowState = DataControlRowState.Edit;
		}

		protected void StatusNodesDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
		{
			e.ObjectInstance = Facade.Instance;
		}

		protected void NextPossibleStatusNodesDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
		{
			e.ObjectInstance = Facade.Instance;
		}

		protected void EditorGrid_RowEditing(object sender, GridViewEditEventArgs e)
		{
			EditorGrid.SelectRow(e.NewEditIndex);
		}

		protected void NextPossibleStatusNodes_DataBinding(object sender, EventArgs e)
		{
			

			
		}

		protected void NextPossibleStatusNodes_DataBound(object sender, EventArgs e)
		{
			var listbox = (ListBox)sender;

			var currentNode = Facade.Instance.GetStatusNodeById((int)EditorGrid.SelectedValue);

			if (currentNode.Workflow.ItemTypes.Count == 0)
			{
				listbox.Enabled = true;
				foreach (var node in currentNode.NextStatusNodes)
				{
					foreach (ListItem item in listbox.Items)
					{
						if (item.Value == node.Id.ToString())
						{
							item.Selected = true;
							break;
						}
					}
				} 
			}
			else
			{
				listbox.Enabled = false;
			}
		}

		protected void EditorGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
		{
			if (CanEdit.Value == "1")
			{
				var node = Facade.Instance.GetStatusNodeById((int)EditorGrid.SelectedValue);
				var selected = (EditorGrid.SelectedRow.Cells[2].Controls[1] as ListBox).GetSelectedIndices();


				for (int i = 0; i < (EditorGrid.SelectedRow.Cells[2].Controls[1] as ListBox).Items.Count; i++)
				{
					if ((EditorGrid.SelectedRow.Cells[2].Controls[1] as ListBox).Items[i].Selected == true)
					{
						var nextId = Convert.ToInt32((EditorGrid.SelectedRow.Cells[2].Controls[1] as ListBox).Items[i].Value);
						Facade.Instance.AddNextStatusNode(node.Id, nextId);
					}
					else
					{
						var disposeNextId = Convert.ToInt32((EditorGrid.SelectedRow.Cells[2].Controls[1] as ListBox).Items[i].Value);
						Facade.Instance.RemoveNextStatusNode(node.Id, disposeNextId);
					}
				}
			}
			
		}

		protected void EditorGrid_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			if (e.CommandName == "Edit")
			{
				var id = Convert.ToInt32(Request["id"]);
				var wf = Facade.Instance.GetWorkFlowById(id);

				if (wf.ItemTypes.Count > 0)
				{
					CanEdit.Value = "0";
				}
				else
				{
					CanEdit.Value = "1";
				}

			}
		}

		protected void btnRemove_Click(object sender, EventArgs e)
		{
			var idToDelete = Convert.ToInt32(CurrentNodes.SelectedValue);
			var node = Facade.Instance.GetStatusNodeById(idToDelete);
			
			if (node.Workflow.ItemTypes.Count > 0)
			{
				litMessage.Text = "No se puede eliminar el estado ya que el flujo al que pertenece está siendo usado por tipos de item.";
			}
			else
			{
				Facade.Instance.RemoveStatusNode(idToDelete);
				EditorGrid.DataBind();
				CurrentNodes.DataBind();
			}
		}

	}
}